package com.pig4cloud.pig.module.controller.raffle;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.raffle.RaffleGoods;
import com.pig4cloud.pig.module.service.raffle.IRaffleGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 抽奖活动奖品Controller
 *
 * @author ruoyi
 * @date 2021-08-29
 */
@Validated
@Api(value = "抽奖活动奖品控制器", tags = {"抽奖活动奖品管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/raffle/goods")
public class RaffleGoodsController {

    private final IRaffleGoodsService raffleGoodsService;

    /**
     * 查询抽奖活动奖品列表
     */
    @ApiOperation("查询抽奖活动奖品列表")
    @PreAuthorize("@pms.hasPermission('module:raffleGoods:list')")
    @GetMapping("/list")
    public TableDataInfo<RaffleGoods> list(@Validated RaffleGoods raffleGoods) {
		QueryWrapper<RaffleGoods> qw = new QueryWrapper<>();
		if(raffleGoods.getRaffleId()!=null){
			qw.eq("raffle_id",raffleGoods.getRaffleId());
		}
        return PageUtils.buildDataInfo(raffleGoodsService.page(PageUtils.buildPage(),qw));
    }
    /**
     * 获取抽奖活动奖品详细信息
     */
    @ApiOperation("获取抽奖活动奖品详细信息")
    @PreAuthorize("@pms.hasPermission('module:raffleGoods:query')")
    @GetMapping("/{id}")
    public R<RaffleGoods> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        RaffleGoods raffleGoods = raffleGoodsService.getById(id);
        return R.ok(raffleGoods);
    }

    /**
     * 新增抽奖活动奖品
     */
    @ApiOperation("新增抽奖活动奖品")
    @PreAuthorize("@pms.hasPermission('module:raffleGoods:add')")
    @SysLog("抽奖活动奖品")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody RaffleGoods raffleGoods) {
        return R.okByRow(raffleGoodsService.save(raffleGoods) ? 1 : 0);
    }

    /**
     * 修改抽奖活动奖品
     */
    @ApiOperation("修改抽奖活动奖品")
    @PreAuthorize("@pms.hasPermission('module:raffleGoods:edit')")
    @SysLog("抽奖活动奖品")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody RaffleGoods raffleGoods) {
        return R.okByRow(raffleGoodsService.updateById(raffleGoods) ? 1 : 0);
    }

    /**
     * 删除抽奖活动奖品
     */
    @ApiOperation("删除抽奖活动奖品")
    @PreAuthorize("@pms.hasPermission('module:raffleGoods:remove')")
    @SysLog("抽奖活动奖品")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(raffleGoodsService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
